/**
* @description : 大案快报 详情
* @descriptionDetail : 大案快报 详情
* @copyright : 浙江烟草
* @author : mc
* @create : 2022-12-05 15:51:34
*/
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<view class="nav-right-icon" v-if="doc.pdfFileId">
				<text @click="viewPdf">
					预览
				</text>
			</view>
		</theme-navbar>
		<!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		<fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->
		<view class="content content-bg ifn-content" style="background-color: white;">
			<u-form class="u-form-m" :model="baseInfo" label-position="top" ref="uForm">
				<u-form-item label="案件名称">
					<u-input v-model="baseInfo.caseName" placeholder="" :disabled="true">
					</u-input>
				</u-form-item>
				<u-form-item label="申请单位">
					<u-input :disabled="true" placeholder="" v-model="baseInfo.orgName"></u-input>
				</u-form-item>
				<u-form-item label="案值(万元)">
					<u-input :disabled="true" placeholder="" v-model="baseInfo.caseTotalVal"></u-input>
				</u-form-item>
				<u-form-item label="案发时间">
					<u-input :disabled="true" v-model="baseInfo.caseDate" placeholder=""></u-input>
				</u-form-item>
				<u-form-item label="案件编号">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.caseCode"></u-input>
				</u-form-item>
				<u-form-item label="情报来源">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.clueSourceName"></u-input>
				</u-form-item>
				<u-form-item label="协作单位">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.cooperateName"></u-input>
				</u-form-item>
				<u-form-item label="案发地">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.casePlace"></u-input>
				</u-form-item>
				<u-form-item label="查获环节">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.getNodeName"></u-input>
				</u-form-item>
				<u-form-item label="品种数">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.goodsCnt"></u-input>
				</u-form-item>
				<u-form-item label="窝点个数">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.denCnt"></u-input>
				</u-form-item>
				<u-form-item label="车辆数">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.carCnt"></u-input>
				</u-form-item>
				<u-form-item label="船舶数">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.shipCnt"></u-input>
				</u-form-item>
				<u-form-item label="嫌疑人数">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.partyCnt"></u-input>
				</u-form-item>
				<u-form-item label="来源省市">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.sourceAddr"></u-input>
				</u-form-item>
				<u-form-item label="去向省市">
					<u-input placeholder="" :disabled="true" v-model="baseInfo.targetAddr"></u-input>
				</u-form-item>
				<fsection title="途径省市"></fsection>
				<u-form-item>
					<u-table>
						<u-tr>
							<u-th width="100%">
								途径省市
							</u-th>
						</u-tr>
						<u-tr v-for="item in addrList" :key="item.index">
							<u-td width="100%">
								{{ item.channelName || '&nbsp;' }}
							</u-td>
						</u-tr>
					</u-table>
				</u-form-item>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_form_item_bottom_border_color"></u-gap>
				<fsection title="物品信息"></fsection>
				<u-table style="margin-top: 10rpx;margin-bottom: 20rpx;">
					<u-tr>
						<u-th>物品类型</u-th>
						<u-th>物品性质</u-th>
						<u-th>计量单位</u-th>
						<u-th>数量</u-th>
					</u-tr>
					<u-tr v-for="item in goodList" :key="item.bigGoodsUuid">
						<u-td>{{ item.goodsTypeName || '&nbsp;' }}</u-td>
						<u-td>{{ item.propTypeName || '&nbsp;' }}</u-td>
						<u-td>{{ item.unit || '&nbsp;' }}</u-td>
						<u-td>{{ item.qty || '&nbsp;' }}</u-td>
					</u-tr>
				</u-table>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_form_item_bottom_border_color"></u-gap>
				<fsection title="上报信息"></fsection>
				<u-form-item label="">
					<u-input placeholder="" type="textarea" :disabled="true" v-model="baseInfo.reportInfo"></u-input>
				</u-form-item>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_form_item_bottom_border_color"></u-gap>
				<fsection title="审核信息"></fsection>
				<view style="padding-left: 32rpx;padding-right:32rpx;">
					<flow-his :approve="approves"></flow-his>
				</view>
			</u-form>
		</view>
	</view>
</template>

<script>
	import fsection from "@/components/form-section.vue";
	import majorDocMixin from "./majorDocMixin.js";
	import bigCaseService from "@/service/case/bigCase.service";
	import commonService from "@/service/common/common.service.js";
	import FlowHis from '@/components/flow-his.vue';
	import {
		GOODS_TYPE,
	} from "@/service/case/mapping/enum.js";
	import fabButton from '@/components/uni-fab.vue';

	export default {
		components: {
			fsection,
			FlowHis,
			fabButton
		},
		mixins: [majorDocMixin, ],
		data() {
			return {
				user: this.$storage.get(this.$rp.common.USER),
				baseInfo: {},
				doc: {},
				goodList: [],
				approves: [],
				apprvList: [],
				addrList: [],
				fContent: [{
					iconPath: require('@/static/sou.png'),
					selectedIconPath: require('@/static/sou.png'),
					text: '预览',
					active: false,
				}],
			}
		},
		onLoad() {
			let parm = this.getRouteParam();
			this.caseParam = {
				id: parm.applyUuid,
				justView: true,
			};
		},
		mounted() {
			this.$modal.showLoading('加载中...');
			this.docBigCaseStep((docInfo) => {
				if (docInfo == undefined) {
					this.$modal.hideLoading();
					return;
				}
				this.doc = docInfo ? docInfo : {};
				if (this.doc.pdfFileId) {
					this.setRightBtns();
				}
				this.initDocData();
				this.flowHis();
			});
		},
		methods: {
			setRightBtns() {
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("预览", () => {
					this.viewPdf();
					this.setRightBtns();
				}, true, true);
				// #endif
			},
			fabtrigger(btn) {
				if (btn.index == 0) {
					this.viewPdf();
				}
			},
			flowHis() {
				if (!this.$u.test.isEmpty(this.doc.docInsUuid)) {
					commonService.flowHis({
						docInsUuid: this.doc.docInsUuid,
					}).then(res => {
						if (res.success)
							this.approves = res.data;
					});
				}
			},
			getGapMarginRowNumber() {
				return "-" + this.$theme.cm_content_paddng_row.replace('rpx', '');
			},
			// 初始化文书信息
			initDocData() {
				commonService.docFields(this.doc.docUuid, true).then(docConfigs => {
					if (docConfigs.success) {
						this.docFields = [...docConfigs.data, ];
						// 获取勘验笔录信息参数
						let parms = {
							id: this.caseParam.id,
							docUuid: this.doc.docUuid,
							bizUuid: this.doc.bizUuid,
							orgLevel: this.user.orgLevel,
						}
						bigCaseService.getDoc(parms).then(res => {
							this.$modal.hideLoading();
							if (!res.success) {
								this.$u.toast(res.msg);
								return;
							}
							let [tiInfo, rs, ] = [{}, {}, ];
							if (res.data.length === 2) {
								tiInfo = res.data[0];
								rs = res.data[1];
								Object.keys(tiInfo).forEach(key => {
									rs[key] = tiInfo[key];
								});
							} else {
								rs = res.data[0];
							}
							this.baseInfo = rs;
							this.goodList = rs.goodsList;
							if (this.baseInfo.addrList && this.baseInfo.addrList.length > 0) {
								this.addrList = this.baseInfo.addrList;
								this.addrList.filter(item => {
									let obj = item;
									commonService.findBscAreaStreetByAreaUuid({
										areaUuid: obj.targetCity,
									}).then((res) => {
										if (res.success) {
											let text = '';
											let info = res.bean;
											if (info.provName) {
												text += info.provName;
											}
											if (info.cityName) {
												text += '/' + info.cityName;
											}
											this.$set(obj, 'channelName', text)
										}
									}).catch(err => {
										console.log(err);
									})
								})
							}
							// 翻译自接口里面拿到的来源 去向 省市code返回文字显示 start
							if (rs.sourceCity) {
								this.baseInfo.sourceAddr = this.getRealAddr(rs.sourceCity);
							} else if (rs.sourceProv) {
								this.baseInfo.sourceAddr = this.getRealAddr(rs.sourceProv);
							} else {
								console.log(111);
							}
							if (rs.targetCity) {
								this.baseInfo.targetAddr = this.getRealAddr(rs.targetCity);
							} else if (rs.targetProv) {
								this.baseInfo.targetAddr = this.getRealAddr(rs.targetProv);
							} else {
								console.log(111);
							}
							// end
							this.buildReportInfo(this.baseInfo);
						}).catch(e => {
							console.log(e);
							this.$modal.hideLoading();
							this.$u.toast("表单请求失败");
						});
					} else {
						this.$modal.hideLoading();
						this.$u.toast(docConfigs.msg);
					}
				}).catch(e => {
					this.$modal.hideLoading();
					this.$u.toast('文书加载失败:' + e);
				});
			},
			getRealAddr(cityCode) {
				// alert(this.$store.state.areaObj['350500'])
				// alert(this.$store.state.areaObj['150100'])
				// if(this.$u.test.isEmpty(provName)) return "";
				// if(this.$modal.isMuniCity(provCode)){
				//   return cityName || '';
				// }else{
				//   return `${provName || ''}${cityName || ''}`;
				// }
				if (this.$store.state.areaObj[cityCode]) {
					return this.$store.state.areaObj[cityCode] || '';
				}
			},
			viewPdf() {
				let pdfIds = this.doc.pdfFileId;
				if (!pdfIds) {
					this.$u.toast("没有文书");
					return;
				}
				this.setRouteParam({
					fileId: pdfIds,
					fileName: "大案快报申请",
					isDownload: true,
				});
				this.$u.route('pages/commonPage/printerComponent');
			},
			buildReportInfo(data) {
				data.goodsCnt = data.goodsCnt || 0;
				let goodsList = data.goodsList || [];
				goodsList.forEach(function(item) {
					// 物品性质名称 为空值时 并且propType也为空 转换报错问题 fsx 2023-11-7 start
					if (!item.propTypeName ) {
						if(item.propType) {
							item.propTypeName = this.$u.dic.getDicNameByKeyCode(this.$u.dic.DIC_KEYS.GOODS_PROP_TYPE, item.propType);
						}
					}
					// 物品性质名称 为空值时 并且propType也为空 转换报错问题 fsx 2023-11-7 end
					if (!item.goodsTypeName) {
						item.goodsTypeName = this.$u.dic.getDicNameByKeyCode(this.$u.dic.DIC_KEYS.S_CIG_GOODS_TYPE, item.goodsType);
					}
				});
				let goodsInfo = {};
				let goodsType = [];
				goodsList.forEach(function(item) {
					let gType = item.goodsType;
					if (gType) {
						let rName = gType;
						// 如果是卷烟 雪茄烟 和新型卷烟则取物品性质.
						if (gType === GOODS_TYPE.CIGAR ||
							gType === GOODS_TYPE.CIGARETEE ||
							gType === GOODS_TYPE.NEW_CIGARETEE) {
							rName = item.propTypeName;
							goodsType.push(rName);
						} else {
							rName = item.goodsTypeName;
							goodsType.push(rName);
						}
						let tag = goodsInfo[rName] || {};
						tag.unit = item.unit;
						tag.cnt = (tag.cnt || 0) * 1 + (item.qty || 0) * 1;
						tag.name = rName;
						goodsInfo[rName] = tag;
					}
				});
				let names = [];
				if (goodsType.length === 1) {
					let titem = goodsInfo[goodsType[0]];
					names.push((titem.cnt + '') + (titem.unit || ''));
				} else {
					for (let name in goodsInfo) {
						let tag = goodsInfo[name];
						names.push(tag.name + (tag.cnt + '') + (tag.unit || ''));
					}
				}
				data.goodsType = goodsType.join("、");
				data.goodsInfo = names.length > 1 ? '其中' + names.join("、") : names.join("");
				if (data.caseDate && data.caseDate.indexOf('年') === -1) {
					data.caseDate = this.$u.date(new Date(data.caseDate).getTime(), "yyyy年mm月dd日 hh时MM分");
				}
				let text = "【${topArea}${orgName}案件快报】${caseDate}，${topArea}${orgName}根据${clueSourceName}，";
				if (data.cooperate) {
					text += '会同${cooperate}，';
				}
				text += "在${getNodeName}环节查获${goodsType}共计${goodsCnt}个品种${goodsInfo}，案值${caseTotalVal}万元。FFFFFFFF";
				if (data.denCnt && data.denCnt > 0) {
					text += "现场捣毁窝点${denCnt}个";
				}
				if (data.carCnt && data.carCnt > 0) {
					text += "，查获车辆${carCnt}辆";
				}
				if (data.shipCnt && data.shipCnt > 0) {
					text += "，船舶${shipCnt}艘";
				}
				if (data.partyCnt && data.partyCnt > 0) {
					text += "，抓获嫌疑人${partyCnt}名";
				}
				if (data.sourceProvName) {
					text += "，货物来源地为${sourceProvName}";
					if (data.sourceCityName) {
						text += "${sourceCityName}";
					}
				}
				if (data.targetProvName) {
					text += "，去向地为${targetProvName}";
					if (data.targetCityName) {
						text += "${targetCityName}";
					}
				}
				if (data.throughProvName) {
					text += "，途经地为${throughProvName}";
					if (data.targetCityName) {
						text += "${throughCityName}";
					}
				}
				let fields = text.match(/\$\{(\S+?)\}/g);
				if (text.indexOf("FFFFFFFF，") > -1) {
					text = text.replace("FFFFFFFF，", "");
				} else {
					text = text.replace("FFFFFFFF", "");
				}
				fields.forEach(function(v, i) {
					let key = v.replace('\${', '').replace('}', '');
					let val = data[key] || '/';
					let reg = new RegExp("\\$\\{" + key + "\\}", "g");
					text = text.replaceAll(reg, val);
				});
				data.reportInfo = text;
			},
		},
	}
</script>

<style scoped lang="scss">
	.page {
		padding: 0;
	}

	.nav-right-icon {
		margin-right: 30rpx;
		font-size: .8rem;
		color: #FFF;
	}

	.content {
		padding: $cm-conent-padding-column $cm-content-paddng-row;
	}

	.btn-other-cls {
		padding-top: 20rpx;
		border-top: 1px solid $u-border-color;
		padding-bottom: 20rpx;
	}

	.submit-btns {
		display: flex;
		bottom: 40rpx;
		padding-left: 40rpx;
		padding-right: 40rpx;
		width: 100%;
		text-align: center;

		.del {
			flex: 1;
		}

		.mit {
			flex: 1;
			margin-left: 40rpx;
		}
	}

	.fsection {
		width: 75% !important;
	}

	.choice {
		position: relative;
		top: 20rpx;
		float: right;
	}

	.tag {
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
	}

	.good-popup-lay {
		padding: $cm-content-paddng-row;
		width: 100%;
		height: 100%;

		&__title {
			font-size: 36rpx;
			font-weight: 600;
			color: $u-main-color;
			margin: 10rpx 0;
			text-align: center;
			width: 100%;
		}

		&__btns {
			width: 100%;
			margin-top: 24rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;

			uni-button {
				flex: 0.28;
			}
		}
	}

	/deep/.u-td {
		height: auto !important;
	}
</style>
